package controllers.participant;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ParticipantService;
import services.RouteService;
import controllers.AbstractController;
import domain.Route;
import forms.RouteForm;

@Controller
@RequestMapping("/route/participant")
public class RouteParticipantController extends AbstractController {
	@Autowired
	private ParticipantService participantService;
	@Autowired
	private RouteService routeService;

	public RouteParticipantController() {
		super();
	}

	// Listing
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int eventId) {
		ModelAndView result;
		Collection<Route> routes = routeService.findByEventId(eventId);

		result = new ModelAndView("route/list");
		result.addObject("requestURI", "route/participant/list.do");
		result.addObject("routes", routes);
		result.addObject("eventId", eventId);
		result.addObject("ordered", false);
		result.addObject("backURI", "/event/participant/edit.do?eventId="
				+ eventId);

		return result;
	}

	@RequestMapping(value = "/ordered/list", method = RequestMethod.GET)
	public ModelAndView orderedList() {
		ModelAndView result;

		Collection<Route> routes = routeService
				.findByParticipantOrderedByRating(participantService
						.findByPrincipal().getId());

		result = new ModelAndView("route/list");
		result.addObject("requestURI", "route//participant/ordered/list.do");
		result.addObject("routes", routes);
		result.addObject("ordered", true);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	// View

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int routeId) {
		ModelAndView result;

		Route route = routeService.findOne(routeId);
		int eventId = route.getEvent().getId();
		RouteForm routeForm = new RouteForm();

		routeForm.setDifficulty(route.getDifficulty());
		routeForm.setName(route.getName());
		routeForm.setRating(route.getRating());
		routeForm.setLength(route.getLength());
		routeForm.setEventId(route.getEvent().getId());

		Boolean isOwner = false;
		Boolean isNew = false;

		result = new ModelAndView("route/edit");
		result.addObject("requestURI", "route/participant/edit.do");
		result.addObject("isOwner", isOwner);
		result.addObject("isNew", isNew);
		result.addObject("routeForm", routeForm);
		result.addObject("backURI", "/route/list.do?eventId=" + eventId);

		return result;

	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(RouteForm routeForm) {
		ModelAndView result;
		result = createEditModelAndView(routeForm, null);
		return result;

	}

	protected ModelAndView createEditModelAndView(RouteForm routeForm,
			String message) {
		ModelAndView result;

		result = new ModelAndView("route/edit");
		result.addObject("routeForm", routeForm);
		result.addObject("message", message);

		return result;

	}
}
